Graphics Window Example is a small example program which contains a reusable class, "GraphicsWindow". The purpose of GraphicsWindow is to provide a window for drawing which can be resized and scrolled. Three simple drawing tools are provided (lines, rectangles and ovals) and a "grabber hand" to allow the image to be moved around in the window in a way similar to Photoshop, etc.
The window has some properties which you may want to alter from other parts of your program, or preset in the window's "Open" event:
Pic (as picture) - the off-screen picture on which all drawing is (ultimately) done. You can draw directly to pic using its graphics property, e.g.:
graphicsWindow.pic.graphics.drawLine 10,10,50,50
picHeight, picWidth (as integer) - the height and width of the drawing area, in pixels. These must be set either in the "open" event of graphicsWindow, or, if you want to have several instances of the window in your program, when you create the instances, e.g.
dim w as graphicsWindow
w=new(graphicsWindow)
w.picWidth=500
w.picHeight=500
picDepth (as integer) - the color bit depth of the window (e.g. 8 bit). Set this at the same time as you set picHeight and picWidth.
DrawColor (as color) - the colour which any drawing will be done with.
DrawWidth (as integer) - the pen width any drawing will be done with.
DrawMode (as string) - the type of drawing to be done. The modes included in the demo are "line", "rect", "oval" and "grabberHand", but more can easily be added (put your code in the "MouseDrag" and "MouseUp" events of theCanvas - the first one to draw temporarily directly to the screen, the second to draw permanently to the off-screen picture).
offPageColor (as color) - the color which is used as a background for any part of the window which is outside the drawing area. This is most likely to be revealed if the user makes the window bigger. If you do not want this area to be revealed, make "graphicsWindow's" maxHeight and maxWidth the same size as the width and height of "pic".
pageEdgeColor (as color) - the color of the edge line drawn around the edge of the drawing area (i.e. a border between the drawing area and the background area described above.
The Demo:
The floating window "toolPalette" just provides a simple interface to demonstrate what graphicsWindow can do. The X and Y coordinates show the position of the mouse relative to the window/canvas, not the picture area (which may be bigger or smaller than the window, depending on the window's size).
What GraphicsWindow Isn't:
GraphicsWindow is pixel-based, not vector-based, but it could be used as front end for a vector-based system. You would need to collect information about the lines, rectangles, etc. drawn in theCanvas's "MouseUp" event, and use this to know if an object has been selected, in order to modify it.
What You Are Allowed to Do with GraphicsWindow:
Simply, anything you like. Feel free to use or modify the code. Use "export window" in the file menu to export the window (!) and then drag it into your project.
You may need to increase the memory allocated to the built application, especially if you use a large off-screen picture area (picHeight, picWidth) or high bit color (picDepth). There is no code to test for out-of-memory
I made graphicsWindow as part of a project I am doing, and I am offering it to others as a little "thank you" to all those who have offered their time and energy to help me and others on the REALbasic email list, and via the REALbasic Cafe on Hotline.
If you have any comments about graphicsWindow, if you use it, or if you make any improvements to it, I would love to know.